
# -*- coding: utf-8 -*-
#   用于配制，包括基本配制，主从单据表配制和单表编辑界面的配制
# @Time    ： 2024/4/15  13:13
# @Auther  ： 徐良
# File     :  configDelegate.py

from PySide6.QtWidgets import QApplication, QStyleOptionButton, QStyle
from baseClass.baseDelegate import BaseDelegate
from PySide6.QtCore import Qt, Signal, QModelIndex
from mesParts.lineEdit.lineEdits import BodyLineEdit1
from mesParts.editPanel.editPanel import EditPanel      # 弹出字符编辑界面
from mesParts.lineEdit.lineEdits import BodyLineEditInt, BodyLineEditFloat, BodyLineEditStr
from mesParts.comboBox.comboBoxs import ComBox_Type, ComBox_WidgetHead, ComBox_Align, \
    ComBox_WidgetBody, ComBox_Widget


class ConfigDelegate(BaseDelegate):
    """
    其它各列都不能编辑
    无选择条件
    option 参数是单选或多选，0是单选，1是多选
    """
    checkBoxSignal = Signal(QModelIndex)

    def __init__(self, args):
        """

        :param args:
        """
        super(ConfigDelegate, self).__init__()

        if 'selOption' in args:
            self.selOption = args['selOption']              # checkBox是单选还是多选
        else:
            self.selOption = None

        if 'widgetList' in args:                            # 网格中的控件，check为自画控制
            self.widgetList = args['widgetList']
        else:
            self.widgetList = []

        if 'isEditList' in args:
            self.isEditList = args['isEditList']
        else:
            self.isEditList = []


    def paint(self, painter, option, index):
        col = index.column()
        if self.widgetList[col] == 'check':  # check 方框
            check_style = QStyleOptionButton()
            check_style.rect = option.rect
            check_style.rect.setX(int(option.rect.x() + option.rect.width() / 2 - 6))
            if index.data() == 1:
                check_style.state = QStyle.State_Enabled | QStyle.State_On
            else:
                check_style.state = QStyle.State_Enabled | QStyle.State_Off
            QApplication.style().drawControl(QStyle.CE_CheckBox, check_style, painter)

        else:
            super(ConfigDelegate, self).paint(painter, option, index)

    def createEditor(self, parent, option, index):

        col = index.column()
        if col >= 0 and self.isEditList[col]:
            if self.widgetList[col] == 'check':
                return None
            elif self.widgetList[col] == 'BodyLineEditInt':
                editor = BodyLineEditInt(parent)
                return editor
            elif self.widgetList[col] == 'BodyLineEditFloat':
                editor = BodyLineEditFloat(parent)
                return editor
            elif self.widgetList[col] == 'BodyLineEditStr':
                editor = BodyLineEditStr(parent)
                return editor
            elif self.widgetList[col] == 'EditPanel':
                editor = EditPanel(parent)
                return editor
            elif self.widgetList[col] == 'ComBox_Type':
                editor = ComBox_Type(parent)
                return editor
            elif self.widgetList[col] == 'ComBox_WidgetHead':
                editor = ComBox_WidgetHead(parent)
                return editor
            elif self.widgetList[col] == 'ComBox_WidgetBody':
                editor = ComBox_WidgetBody(parent)
                return editor

            elif self.widgetList[col] == 'ComBox_Widget':
                editor = ComBox_Widget(parent)
                return editor

            elif self.widgetList[col] == 'ComBox_Align':
                editor = ComBox_Align(parent)

                return editor
            else:
                return None

    def editorEvent(self, event, model, option, index):
        """

        :param editor:
        :param model:
        :param option:
        :param index:
        :return:
        """
        col = index.column()
        if self.widgetList[col] == 'check':
            if event.button() == Qt.LeftButton:
                if index.data():
                    model.setData(index, 0)
                    self.checkBoxSignal.emit(index)
                else:
                    if self.selOption == 0:
                        if col == 0:
                            self.clearModelSel(model)
                        model.setData(index, 1)
                        self.checkBoxSignal.emit(index)
                    else:
                        model.setData(index, 1)
                        self.checkBoxSignal.emit(index)
            return True

        if self.widgetList[col] == 'EditPanel':

            if event.button() == Qt.LeftButton:
                # 获取字段值
                value = model.data(index, Qt.DisplayRole)
                # 获取字段名
                hn = model.headerData(col, Qt.Horizontal, Qt.DisplayRole)  # 修复了此处的错误
                paraMap = {'value': value, 'index': index, 'model': model}

                self.editPanel = EditPanel(paraMap)
                self.editPanel.setWindowTitle('正在编辑【' + hn + '】字段')
                self.editPanel.editContent.connect(self.makeRet)
                self.editPanel.show()

        return super(ConfigDelegate, self).editorEvent(event, model, option, index)

    def setEditorData(self, editor, index):
        col = index.column()
        if self.widgetList[col] == 'ComBox_Type' or self.widgetList[col] == 'ComBox_WidgetHead' or \
                self.widgetList[col] == 'ComBox_Align' or self.widgetList[col] == 'ComBox_WidgetBody' \
                or self.widgetList[col] == 'ComBox_Widget':
            m = index.model().data(index, Qt.EditRole)
            for i in range(0, editor.count()):
                e = editor.itemText(i)
                if e == m:
                    editor.setCurrentIndex(i)
                    break

        super(ConfigDelegate, self).setEditorData(editor, index)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)  # 设置编辑器的大小为单元格大小


    def makeRet(self, retMap):
        model = retMap['model']
        index = retMap['index']
        value = retMap['value']
        model.setData(index, value, Qt.EditRole)

    def clearModelSel(self, model):
        """
        清除选择
        :return:
        """
        for i in range(0, model.rowCount()):
            index = model.index(i, 0)
            model.setData(index, 0)

